package com.ash.android_adk_demo

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.hc.schedule.model.Day


class DaysAdapter(private val days: Array<Day>, private val taskClickListener: OnTaskClickListener? = null,val currDay:Int=0) : RecyclerView.Adapter<DaysAdapter.DayViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DayViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(
            R.layout.item_day, parent, false)
        return DayViewHolder(view)
    }

    override fun onBindViewHolder(holder: DayViewHolder, position: Int) {
        holder.bind(days[position])
    }

    override fun getItemCount(): Int = days.size

    inner class DayViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val tvDayIndex: TextView = itemView.findViewById(R.id.tv_day_index)
        private val rvTasks: RecyclerView = itemView.findViewById(R.id.rv_tasks)

        fun bind(day: Day) {
            tvDayIndex.text = "第${day.index}天"+if(day.index==currDay) " (今日)" else "(不是今日)"

            // 设置任务列表
            val tasksAdapter = TasksAdapter(day.taskList, if (day.index != currDay) null else taskClickListener)
            rvTasks.apply {
                layoutManager = LinearLayoutManager(itemView.context)
                adapter = tasksAdapter
            }
        }
    }
}